using System.Globalization;
using System.Text;
using System.Web.Mvc.Ajax;
namespace ExtendedMvc.Ajax {
	public static class JavaScriptHelper {

		public const string SubmitWithValidation = @"javascript: if(!document.forms['{0}'].onsubmit || document.forms['{0}'].onsubmit()) {{ document.forms['{0}'].submit(); }}";
		public const string Submit = @"javascript: document.forms['{0}'].submit();";
		public const string Reset = @"javascript: document.forms['{0}'].reset();";

		public const string ClickEventHandler = @"Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));";
		public const string SubmitEventHandler = @"Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), {0});";


		private static string PropertyStringIfSpecified(string propertyName, string propertyValue) {
			if (!string.IsNullOrEmpty(propertyValue)) {
				string str = propertyValue.Replace("'", @"\'");
				return string.Format(CultureInfo.InvariantCulture, " {0}: '{1}',", new object[] { propertyName, str });
			}
			return string.Empty;
		}

		private static string EventStringIfSpecified(string propertyName, string handler) {
			if (!string.IsNullOrEmpty(handler)) {
				return string.Format(CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),", propertyName, handler.ToString());
			}
			return string.Empty;
		}

		private static string InsertionModeToString(InsertionMode insertionMode) {
			switch (insertionMode) {
				case InsertionMode.InsertBefore:
					return "Sys.Mvc.InsertionMode.insertBefore";

				case InsertionMode.InsertAfter:
					return "Sys.Mvc.InsertionMode.insertAfter";

				default:
					return "Sys.Mvc.InsertionMode.replace";
			}
		}

		private static string ToJavascriptString(AjaxOptions ajaxOptions) {
			StringBuilder Builder = new StringBuilder("{");
			Builder.Append(string.Format(CultureInfo.InvariantCulture, " insertionMode: {0},", InsertionModeToString(ajaxOptions.InsertionMode)));
			Builder.Append(PropertyStringIfSpecified("confirm", ajaxOptions.Confirm));
			Builder.Append(PropertyStringIfSpecified("httpMethod", ajaxOptions.HttpMethod));
			Builder.Append(PropertyStringIfSpecified("loadingElementId", ajaxOptions.LoadingElementId));
			Builder.Append(PropertyStringIfSpecified("updateTargetId", ajaxOptions.UpdateTargetId));
			Builder.Append(PropertyStringIfSpecified("url", ajaxOptions.Url));
			Builder.Append(EventStringIfSpecified("onBegin", ajaxOptions.OnBegin));
			Builder.Append(EventStringIfSpecified("onComplete", ajaxOptions.OnComplete));
			Builder.Append(EventStringIfSpecified("onFailure", ajaxOptions.OnFailure));
			Builder.Append(EventStringIfSpecified("onSuccess", ajaxOptions.OnSuccess));
			Builder.Length--;
			Builder.Append(" }");
			return Builder.ToString();
		}

		public static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat) {
			string ajaxScript = ToJavascriptString(ajaxOptions);
			return string.Format(CultureInfo.InvariantCulture, scriptFormat, ajaxScript);
		}
	}
}
